/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model;

import br.com.jr2tecnologia.model.entity.Page;
import br.com.jr2tecnologia.model.entity.Role;
import br.com.jr2tecnologia.model.persistence.DAOFactory;
import br.com.jr2tecnologia.model.repository.RoleRepository;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author ronildo
 */

@Stateless
public class RolePersistence implements RolePersistenceLocal{
    
    @PersistenceContext
    private EntityManager em;
    private RoleRepository roleRepository;

    @PostConstruct
    public void initialize(){
        roleRepository = DAOFactory.getRoleRepository(em);
    }

    public void persist(Role entity) {
        roleRepository.persist(entity);
    }

    public void update(Role entity) {
        roleRepository.update(entity);
    }

    public void remove(Role entity) {
        roleRepository.remove(entity, entity.getPkRole());
    }

    public Role findById(Long id){
        return roleRepository.findById(id);
    }

    public Role findByName(String name){
        return roleRepository.findByName(name);
    }

    public List<Role> list(Role role){
        return roleRepository.list(role);
    }

    public List<Role> listAll(){
        return roleRepository.listAll();
    }

    public List<Page> listPagesOfRole(Role entity){
        Role role = roleRepository.findById(entity.getPkRole());
        return new LinkedList<Page>(role.getPageList());
    }
}
